import axios from '@/utils/request'
const STATUS = {
  '-1': '已取消',
  0: '未支付',
  1: '已支付'
}

export default {
  FinanceFee: {
    entity: { name: 'Fee', prefix: `manage` },
    list: {
      export: {
        query: {
          '@display': `{
            'id' : 'entity.getId()',
            'store' : 'entity.getStore().getName()',
            'payer' : 'entity.getPayer().getUsername()',
            'receiver' : 'entity.getReceiver().getUsername()',
            'amount' : 'entity.getAmount()',
            'status' : '${JSON.stringify(STATUS)}[entity.getStatus()]',
            'comment' : 'entity.getComment()',
            'paidTime' : 'entity.getPaidTime().format("Y-m-d H:i:s")',
            'createdTime' : 'entity.getCreatedTime().format("Y-m-d H:i:s")'
          }`,
          limit: 10000000
        },
        label: {
          'id': 'id',
          'store': '额外信息',
          'payer': '付款方',
          'receiver': '收款方',
          'amount': '金额',
          'status': '状态',
          'comment': '备注',
          'paidTime': '支付时间',
          'createdTime': '创建时间'
        }
      },
      disabled_actions: ['edit', 'delete'],
      actions: [
        {
          name: 'ctrl',
          position: 'list',
          component: {
            props: ['record', 'refresh'],
            data() {
              return {
                loading: false
              }
            },
            render(h) {
              if (this.record.type === 'blackhole-fee' && this.record.status <= 0) {
                return <div>
                  <el-popconfirm title='确定执行当前记录？'
                    {...{
                      on: {
                        onConfirm: (val) => {
                          this.loading = true
                          axios.post(`/api/fees/${this.record.id}/perform`).then((res) => {
                            this.$message.success('执行成功！')
                            this.refresh()
                          }).finally(() => {
                            this.loading = false
                          })
                        }
                      }
                    }}>
                    <el-button slot='reference' size='small' type='success' icon='el-icon-check' loading={this.loading} plain>执行</el-button>
                  </el-popconfirm>

                  <router-link to={`${this.record.id}/update`}>
                    <el-button style='margin-left:5px' size='small' icon='el-icon-edit' plain>
                    修改
                    </el-button>
                  </router-link>

                  <el-popconfirm title='确定删除当前记录？'
                    {...{
                      on: {
                        onConfirm: (val) => {
                          this.loading = true
                          axios.delete(`/manage/fees/${this.record.id}`).then((res) => {
                            this.$message.success('删除成功！')
                            this.refresh()
                          }).finally(() => {
                            this.loading = false
                          })
                        }
                      }
                    }}>
                    <el-button style='margin-left:5px' slot='reference' size='small' type='danger' icon='el-icon-delete' loading={this.loading} plain>删除</el-button>
                  </el-popconfirm>
                </div>
              }
            }
          }
        }
      ],
      query: {
        '@filter': 'entity.getType() == "blackhole-fee"',
        '@order': 'entity.id|DESC'
      },
      list_filter: {
        'store.id': () => {
          return axios
            .get(`/manage/stores`)
            .then(res =>
              Object.assign({ __label: '门店' },
                ...res.data.map(v => { return { [v.id]: v.name } })))
        },
        status: Object.assign({ __label: '状态' }, STATUS)
      },
      list_display: [
        'id',
        { property: 'store', label: '额外信息' },
        { property: 'payer', label: '付款方' },
        { property: 'receiver', label: '收款方' },
        { property: 'amount', label: '金额', editable: true },
        {
          property: 'status',
          component: {
            props: ['data'],
            data() {
              return {
                status: STATUS
              }
            },
            render(h) {
              return <div>{this.status[this.data]}</div>
            }
          }
        },
        'comment',
        'paidTime',
        'createdTime'
      ]
    },
    form: {
      fields: [
        {
          property: 'store',
          field_options: { label: '门店' },
          required: false,
          component: {
            props: ['form', 'property'],
            data() {
              return { data: [] }
            },
            created() {
              axios.get(`/manage/stores`, {
                params: {
                  'limit': 1000000
                }
              }).then((res) => {
                this.data = res.data
              })
            },
            render(h) {
              return (
                <el-select v-model={this.form[this.property]} clearable filterable onChange={() => { this.form['staff'] = null }}>{
                  Object.assign(this.data).map(v => {
                    return <el-option key={v.id} label={v.name} value={v.id} />
                  })
                }</el-select>
              )
            }
          }
        },
        {
          property: 'payer',
          field_options: { label: '付款方' },
          default_value: 105,
          component: {
            props: ['form', 'property'],
            data() {
              return { data: [] }
            },
            render(h) {
              return (
                <el-input v-model={this.form[this.property]} disabled={true}></el-input>
              )
            }
          }
        },
        {
          property: 'receiver',
          field_options: { label: '收款方' },
          default_value: 239,
          component: {
            props: ['form', 'property'],
            data() {
              return { data: [] }
            },
            render(h) {
              return (
                <el-input v-model={this.form[this.property]} disabled={true}></el-input>
              )
            }
          }
        },
        { property: 'amount', field_options: { label: '金额' }},
        {
          property: 'type',
          default_value: 'blackhole-fee',
          component: {
            props: ['form', 'property'],
            data() {
              return { data: [] }
            },
            render(h) {
              return (
                <el-input v-model={this.form[this.property]} disabled={true}></el-input>
              )
            }
          }
        },
        { property: 'comment', type: 'textarea' }
      ]
    }
  }
}

